package com.znyj.servlet.mainController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.znyj.servlet.constant.SysConstant;
import com.znyj.servlet.entity.UserEntity;
import com.znyj.servlet.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;

/**
 * Created by gaoliwei02 on 17/7/5.
 */
@Controller
@RequestMapping(value = "/sign")
public class SignController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/login.inner", method = RequestMethod.POST)
    protected @ResponseBody String loginInner(@RequestBody String receiveData, HttpSession session) throws Exception {
        JSONObject getJson = JSON.parseObject(receiveData);
        JSONObject putJson = new JSONObject();

        String username = getJson.getString("username");
        String password = getJson.getString("password");
        String ipAddress = getJson.getString("ipAddress");

        //查找username的数据库记录
        UserEntity userEntity = userService.findByUsername(username);
        if (userEntity == null) {
            //没有注册
            putJson.put("status", "notExist");
        } else {
            if (userEntity.getPassWord().equals(password)) {
                //密码对上了，登录成功
                putJson.put("status", "loginOk");
                session.setAttribute("username", userEntity.getUserName());
                session.setAttribute("rolePermission", userEntity.getRolePermission());
                session.setMaxInactiveInterval(SysConstant.SESSION_TIMEOUT);
                userService.updateUserInfo(new Timestamp(System.currentTimeMillis()),0,ipAddress,username);
            } else {
                //密码错误
                putJson.put("status", "keyWrong");
            }
        }
        return putJson.toString();
    }

    @RequestMapping(value = "/signup.inner", method = RequestMethod.POST)
    protected @ResponseBody String signupInner(@RequestBody String receiveData, HttpSession session) throws ParseException, IOException {
        JSONObject getJson = JSON.parseObject(receiveData);
        JSONObject putJson = new JSONObject();

        String username = getJson.getString("username");
        String password = getJson.getString("password");
        String phonenumber = getJson.getString("phonenumber");
        String note = getJson.getString("note");
        String ip = getJson.getString("ip");
        UserEntity userEntity = new UserEntity();
        userEntity.setUserName(username);
        userEntity.setPassWord(password);
        userEntity.setPhoneNumber(phonenumber);
        userEntity.setNote(note);

        userEntity.setLastSignTime(new Timestamp(System.currentTimeMillis()));
        userEntity.setRegisterTime(new Timestamp(System.currentTimeMillis()));
        userEntity.setSignStatus(0);
        userEntity.setIpAddress(ip);
        userEntity.setRolePermission(1);

        //查找username的数据库记录

        if (null == userService.findByUsername(username)) {
            userService.insertUser(userEntity);
            putJson.put("register", "ok");
        } else {
            putJson.put("register", "exist");
        }
        return putJson.toString();
    }

    @RequestMapping(value = "/getusername.inner", method = RequestMethod.POST)
    protected @ResponseBody String getusernameInner(HttpSession session) throws Exception {
        JSONObject putJson = new JSONObject();
        String username = (String) session.getAttribute("username");
        putJson.put("username", username);
        return putJson.toString();
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logoutPage(HttpSession session) throws Exception {
        //清除Session
        session.invalidate();
        return "/page/sign_page";
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(HttpSession session) throws Exception {
        return "/page/sign_page";
    }

}
